import axios from 'axios'
import { RootThunkAction } from '..'
export type Channel = {
  id: number
  name: string
}
export type ChannelAction =
  | {
      type: 'channel/getChannelList'
      payload: Channel[]
    }
  | {
      type: 'channel/changeActive'
      payload: number
    }
export function getChannelList(): RootThunkAction {
  return async (dispatch) => {
    const res = await axios.get('http://toutiao.itheima.net/v1_0/channels')
    dispatch({
      type: 'channel/getChannelList',
      payload: res.data.data.channels
    })
  }
}

export function changeActive(id: number): RootThunkAction {
  return (dispatch) => {
    dispatch({
      type: 'channel/changeActive',
      payload: id
    })
  }
}
